<!--
 * @Author: your name
 * @Date: 2021-10-20 10:49:42
 * @LastEditTime: 2021-10-21 21:52:03
 * @LastEditors: Please set LastEditors
 * @Description: In User Settings Edit
 * @FilePath: \vue_test3\src\components\goods\List.vue
-->
<template>
  <div>
       <!-- 面包屑导航 -->
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>商品管理</el-breadcrumb-item>
      <el-breadcrumb-item>商品列表</el-breadcrumb-item>
    </el-breadcrumb>

    <!-- 卡片视图 -->
    <el-card>
        <!-- 搜索与添加区域 -->
        <el-row>
            <el-col :span="5">
            <el-input
                placeholder="请输入内容"
                v-model="queryInfo.query"
                clearable
                @clear="getGoodslist"
            >
                <el-button
                slot="append"
                icon="el-icon-search"
                @click="getGoodslist"
                ></el-button>
            </el-input>
            </el-col>
            <el-col :span="1">
            <el-button type="primary" @click="goAddPage">添加</el-button>
            </el-col>
        </el-row>

        <!-- table表格区域 -->
            <el-table :data="goodsList" border stripe>
            <el-table-column type="index" label="ID"></el-table-column>
            <el-table-column label="商品名称" prop="goods_name"></el-table-column>
            <el-table-column label="商品价格" prop="goods_price" width="90px"></el-table-column>
            <el-table-column label="商品重量" prop="goods_weight" width="90px"></el-table-column>
            <el-table-column label="创建时间" prop="add_time" width="160px" :formatter="dateFormat"></el-table-column>
            <el-table-column label="操作" width="130px">
            <template v-slot="scope">
                <!-- 编辑按钮 -->
                <el-button
                type="primary"
                icon="el-icon-edit"
                size="mini"
                
                ></el-button>
                <!-- 删除按钮 -->
                <el-button
                type="danger"
                icon="el-icon-delete"
                size="mini"
                @click="removeGoodsById(scope.row.goods_id)"
                ></el-button>
            </template>
            </el-table-column>
        </el-table>

           <!-- 分页区域 -->
            <el-pagination
                @size-change="handleSizeChange"
                @current-change="handleCurrentChange"
                :current-page="queryInfo.pagenum"
                :page-sizes="[1, 2, 5, 10]"
                :page-size="queryInfo.pagesize"
                layout="total, sizes, prev, pager, next, jumper"
                :total="total"
                background
            >
            </el-pagination>

    


      </el-card>



      
  </div>
</template>

<script>
import moment from 'moment'
export default {
    created(){
        this.getGoodslist()
    },
    data() {
        return {
            queryInfo: {
                    query: "",
                    //    当前的页数
                    pagenum: 1,
                    // 当前每页限时多少条数据
                    pagesize: 15,
                },
            //商品列表
            goodsList: [],
            total: 0,

        }
    },
    methods:{
        async getGoodslist(){
             const { data: res } = await this.$http.get("goods", {
                params: this.queryInfo,
            })
            if (res.meta.status !== 200)
                return this.$message.error("获取用户列表失败")
                // console.log(res.data)
            this.goodsList = res.data.goods
            this.total = res.data.total
            },

        //时间格式化
            dateFormat(row, column) {
            var date = row[column.property];
            if (date == undefined) {
                return "";
            }
            return moment(date).format("YYYY-MM-DD HH:mm:ss");
            },
        //时间格式化
        
            // 监听pagesize改变事件
            handleSizeChange(newSize) {
            //   console.log(newSize)
            this.queryInfo.pagesize = newSize
            this.getGoodslist()
            },
            //监听页码值的改变
            handleCurrentChange(newPage) {
            //   console.log(newPage)
            this.queryInfo.pagenum = newPage
            this.getGoodslist()
            },

             // 根据Id删除对应的商品信息
            async removeGoodsById(id) {
            // 弹框询问用户是否删除数据
            const confirmResult = await this.$confirm(
                '此操作将永久删除该用户, 是否继续?',
                '提示',
                {
                confirmButtonText: '确定',
                cancelButtonText: '取消',
                type: 'warning'
                }
            ).catch(err => err)

            // 如果用户确认删除，则返回值为字符串 confirm
            // 如果用户取消了删除，则返回值为字符串 cancel
            // console.log(confirmResult)
            if (confirmResult !== 'confirm') {
                return this.$message.info('已取消删除')
            }

            const { data: res } = await this.$http.delete(`goods/${id}`)

            if (res.meta.status !== 200) {
                return this.$message.error('删除商品失败！')
            }

            this.$message.success('删除商品成功！')
            //刷新商品表
            this.getGoodslist()
            },

            // 添加商品
            goAddPage(){
                this.$router.push('goods/add')
            }

    },
}
</script>

<style>

</style>